1 package org.smartcomps.twister.tools;
2
3 import junit.framework.TestCase;
4 import org.smartcomps.twister.common.persistence.XMLSessionManager;
5
6 import org.dom4j.*;
7 import org.dom4j.io.SAXReader;
8 import org.dom4j.datatype.DatatypeElement;
9 import org.dom4j.datatype.DatatypeDocumentFactory;
10
11 import java.util.Calendar;
12 import java.util.List;
13 import java.util.Date;
14 import java.math.BigInteger;
15
16 import com.sun.msv.datatype.xsd.DatatypeFactory;
17 import com.sun.msv.datatype.xsd.XSDatatype;
18 import com.sun.msv.datatype.xsd.DateTimeType;
19 import com.sun.msv.datatype.xsd.datetime.*;
20
21 /***
22 * Tests rules execution in DOM4J and Xindice (when it will support it).
23 */
24 public class TestRule extends TestCase {
25
26 private XMLSessionManager xmlSessionManager = new XMLSessionManager();
27
28 protected void setUp() throws Exception {
29 xmlSessionManager.initialize();
30 xmlSessionManager.beginTransaction();
31 }
32
33 protected void tearDown() throws Exception {
34 xmlSessionManager.commitTransaction();
35 }
36
37 public void testEquality() throws Exception {
38 Document testDoc = DocumentHelper.createDocument();
39 Element root = testDoc.addElement("root");
40 root.addElement("elmt1").setText("testText");
41 root.addElement("elmt2").setText("testText");
42
43 Boolean result = (Boolean)root.selectObject("/root/elmt1=/root/elmt2");
44 System.out.println("-> " + result);
45 assertTrue("Equality didn't work properly", result.booleanValue());
46
47 // Collection testColl = XMLDataAccess.getCollection("/testColl");
48 // if (testColl == null) {
49 // testColl = XMLDataAccess.createCollection(XMLDataAccess.getRootCollection(), "testColl");
50 // }
51 //
52 // XMLDataAccess.insertDocument(testColl, "testRuleDoc", testDoc);
53 //
54 // XPathQueryService queryService = (XPathQueryService) testColl.getService("XPathQueryService", "1.0");
55 // ResourceSet result = queryService.queryResource("testRuleDoc", "/root/elmt1=/root/elmt2");
56 // System.out.println("Found results : " + result.getSize());
57 // ResourceIterator resultIter = result.getIterator();
58 // while (resultIter.hasMoreResources()) {
59 // Resource res = resultIter.nextResource();
60 // if (res instanceof XMLResource) {
61 // System.out.println("Resource found : " + ((XMLResource)res).toString());
62 // } else {
63 // System.out.println("Resource found : " + res);
64 // }
65 // }
66 }
67
68 public void testAddition() throws Exception {
69 Document testDoc = DocumentHelper.createDocument();
70 Element root = testDoc.addElement("root");
71 root.addElement("elmt1").setText("1");
72 root.addElement("elmt2").setText("3");
73 root.addElement("elmt3").setText("4");
74
75 Boolean result = (Boolean)root.selectObject("/root/elmt1+/root/elmt2=/root/elmt3");
76 System.out.println("-> " + result);
77 assertTrue("Addition didn't work properly", result.booleanValue());
78 }
79
80 public void testInferior() throws Exception {
81 Document testDoc = DocumentHelper.createDocument();
82 Element root = testDoc.addElement("root");
83 root.addElement("elmt1").setText("1");
84 root.addElement("elmt2").setText("3");
85 root.addElement("elmt3").setText("5");
86
87 Boolean result = (Boolean)root.selectObject("/root/elmt1+/root/elmt2</root/elmt3");
88 System.out.println("-> " + result);
89 assertTrue("Addition didn't work properly", result.booleanValue());
90 }
91
92 public void testEmptyDoc() throws Exception {
93 Document testDoc = DocumentHelper.createDocument();
94 Element root = testDoc.addElement("root");
95 // root.addElement("elmt1").setText("1");
96 // root.addElement("elmt2").setText("3");
97 // root.addElement("elmt3").setText("5");
98
99 Boolean result = (Boolean)root.selectObject("1+1=2");
100 System.out.println("-> " + result);
101 assertTrue("Addition didn't work properly", result.booleanValue());
102 }
103
104 public void testDate() throws Exception {
105 Calendar now = Calendar.getInstance();
106 String nowStr = DatatypeFactory.getTypeByName("dateTime").serializeJavaObject(now, null);
107
108 DatatypeDocumentFactory docFactory = new DatatypeDocumentFactory();
109 Document testDoc = docFactory.createDocument();
110
111 Element root = testDoc.addElement("root");
112 QName dateQName = new QName("date");
113 IDateTimeValueType dateValue = DateTimeFactory.createFromDateTime(
114 BigInteger.valueOf(now.get(Calendar.YEAR)),
115 new Integer(now.get(Calendar.MONTH)),
116 new Integer(now.get(Calendar.DAY_OF_MONTH)),
117 new Integer(now.get(Calendar.HOUR_OF_DAY)),
118 new Integer(now.get(Calendar.MINUTE)),
119 new Integer(now.get(Calendar.SECOND)),
120 TimeZone.create(0));
121 DatatypeElement dateElmt = new DatatypeElement(dateQName, DatatypeFactory.getTypeByName("dateTime"));
122 dateElmt.setData(dateValue);
123 System.out.println("Date : " + dateElmt.asXML());
124 root.add(dateElmt);
125
126 QName durationQName = new QName("duration");
127 Integer zero = new Integer(0);
128 ITimeDurationValueType durationValue = TimeDurationFactory.create(
129 zero, zero, new Integer(2), zero, zero, zero);
130 DatatypeElement durationElmt = new DatatypeElement(durationQName, DatatypeFactory.getTypeByName("duration"));
131 durationElmt.setData(durationValue);
132 System.out.println("Duration : " + durationElmt.asXML());
133 root.add(durationElmt);
134
135 // XPath xPathSelector = DatatypeDocumentFactory.getInstance().createXPath("/root/date + /root/duration");
136 XPath xPathSelector = DatatypeDocumentFactory.getInstance().createXPath("/root/date");
137 Object result = xPathSelector.evaluate(testDoc);
138 System.out.println("=> Result : " + result);
139 }
140
141 public void testDuration() throws Exception {
142 XSDatatype durationType = DatatypeFactory.getTypeByName("duration");
143 BigTimeDurationValueType duration =
144 (BigTimeDurationValueType) durationType.createJavaObject("P1Y2M3DT10H30M", null);
145 // duration.
146 // System.out.println("=> " + obj.getClass().getName());
147 }
148 }
This page was automatically generated by Maven